﻿#include <iostream>
#include <vector>

using namespace std;

class Solution
{
public:
    // 14. 最长公共前缀
    // 两两对比
    string longestCommonPrefix(vector<string>& strs)
    {
        string ret = strs[0];
        for (int i = 1; i < strs.size(); i++)
            ret = findComon(ret, strs[i]);
        return ret;
    }

    string findComon(string s1, string s2)
    {
        int i = 0;
        while (i < min(s1.size(), s2.size()) && s1[i] == s2[i])
            i++;
        return s1.substr(0, i);
    }

    // 统一比较
    /*string longestCommonPrefix(vector<string>& strs)
    {
        for (int i = 0; i < strs[0].size(); i++)
        {
            char tmp = strs[0][i];
            for (int j = 1; j < strs.size(); j++)
                if (i == strs[j].size() || tmp != strs[j][i])
                    return strs[0].substr(0, i);
        }
        return strs[0];
    }*/
};

int main()
{
    vector<string> v{ "flower","flow","flight" };
    cout << Solution().longestCommonPrefix(v) << endl;

    return 0;
}